/**
 * 发票 - 编辑新增（选择开票申请）
 * @author liuw
 * @date 2019/09/04
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table_where = {},
        vueData = {
            isAdd: false,
            isCheck: false,
            type: 1,
            form: {
                GWEDDOCNO : null,
                GWEDDOCDATE : null,
                GWED004_NAME: null
            }
        },
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field : "GWEDDOCNO", title: "申请单号",width: 150},
            {field : "GWEDDOCDATE", title: "申请日期",width: 100},
            {field : "GWED001", title: "申请人",hide:true},
            {field : "GWED001_NAME", title: "申请人",width: 100},
            {field : "GWED002", title: "发票种类",width: 100,filterDatas: app.datas_('crm_gwei006_gwed002')},
            {field : "GWED003", title: "出货单号",width: 180},
            {field : "GWED102", title: "出货单项次",width: 100},
            {field : "GWEC003", title: "ERP订单号",width: 150},
            {field : "GWEC101", title: "订单项次",width: 100},
            {field : "GWED004_NAME", title: "客户",width: 100},
            {field : "GWEDSEQ", title: "项次",width: 100},
            {field : "GWED008", title: "产品编号",width: 100},
            {field : "GWAML003", title: "品名",width: 100},
            {field : "GWAML004", title: "规格",width: 100},
            {field : "GWED009", title: "数量",width: 100},
            {field : "GWEF009_SUM", title: "已开票数量",width: 100},
            {field : "GWED010", title: "单价",width: 100},
            {field : "GWED011", title: "金额",width: 100},
            {field : "GWED013", title: "备注",width: 100},
            {field : "GWEDACTI", title: "有效否",width: 100,filterDatas: app.datas_('sys_y_n')},
            {field : 'GWED004', hide: true},//客户
            {field : 'GWAP005', hide: true},//币种
            {field : 'GWAP014', hide: true},//税种
            {field : 'GWEA006', hide: true},//税率
            {field : 'GWAK057', hide: true},//汇率类型
        ]],
        callObj,
        listData = null,
        table, gwei007EditAddTable,cb,layOpenIndex
    ;

    var Gwei007EditAdd = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwei007_edit_add',
                    _searchs: [
                        { id: 'gwei007_edit_add', tableId: 'gwei007_edit_add_table', datas:  cols[0] }
                    ],
                    _loadTplsFull: []
                }, win);
            },
            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwei007EditAddTable = table.render({
                        elem: app.getByid(doc, 'gwei007_edit_add_table'),
                        id: 'gwei007_edit_add_table',
                        url: g_ctxPath + 'crm/gwei007/getGwedData',
                        height:'240',
                        cols: cols,
                        data: listData,
                        totalRow: true,
                        page: true,
                        where: table_where,
                        done: function (res, curr, count) {

                        }

                    });
                });

            },

            //表格重载
            reloadTableCurr: function () {
                self._reloadTable({id: 'gwei007_edit_add_table', where: {
                        GWEFDOCNO : callObj.obj.GWEFDOCNO,
                        GWED004 : callObj.obj.GWEF002,
                        GWEDPLANT : callObj.obj.GWEFPLANT,
                        gwedlist : callObj.obj.gwedlist,
                        GWEDDOCDATE: vueData.form.GWEDDOCDATE,
                        GWEDDOCNO: vueData.form.GWEDDOCNO
                }});
            },

            _call: function (tagData) {
                callObj = tagData
                cb = callObj.cb;
                layOpenIndex = callObj.layOpenIndex;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.obj,
                    reset: true
                });
                vueData.form.GWED004_NAME = callObj.obj.GWEF002_NAME;

                table_where.GWEFDOCNO = callObj.obj.GWEFDOCNO;
                table_where.GWED004 = callObj.obj.GWEF002;
                table_where.GWEDPLANT = callObj.obj.GWEFPLANT;
                table_where.gwedlist = callObj.obj.gwedlist;

                self.initTable();
            },

            _ready: function () {
                table = layui.table;
                var vue = new Vue({
                    el: '#gwei007_edit_add',
                    data: vueData,
                    methods: {
                        cancel: function () {
                            layer.close(layer.index);
                        },
                        saveData: function () {
                            // 将选中的数据添加到编辑页面中的表格中
                            let checkStatusObj = table.checkStatus('gwei007_edit_add_table');
                            if(checkStatusObj.data.length == 0) {
                                app.showWarning("至少选择一笔数据进行保存");
                                return;
                            }
                            cb(checkStatusObj.data);
                            layer.close(layer.index);
                        },
                        queryData: function () {
                            self.reloadTableCurr();
                        },
                        resetData: function () {
                            vueData.form.GWEDDOCDATE = '';
                            vueData.form.GWEDDOCNO = '';
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwei007_edit_add');
                    }
                });
            },
            _lazy: function () {
                this._super();
            }
        })
    ;
    return new Gwei007EditAdd();
})
;